#include <stdio.h>
#include <sys/types.h>         
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "admin_API.h"
#include "public.h"

#define ERR_FLAG(info) do{printf("%s:%s:%d>>%s\n",__FILE__,__func__,__LINE__,info);}while(0)
#define DEBUG_JUMP 0
static void get_other_char(void);
int do_find_byname(int sockfd,int usermode,staff_info_TypeDef *info_o);
int do_modify_byname(int sockfd,int usermode);
int do_add_user(int sockfd);
int do_del_user_by_id(int sockfd);
int do_find_history(int sockfd);

static char name[20];
static char pwd[20];
struct sockaddr_in server_addr;
//static char identity = 0;
//封装传输结构体，结构体中需要包含，操作码，账号，密码，员工个人信息

//用户模式界面
void UI_Mode()
{
	printf("*************************************************************\n");
	printf("********  1：管理员模式    2：普通用户模式    3：退出********\n");
	printf("*************************************************************\n");
}

//选择管理员模式界面
void UI_Mode_Root() 
{ 
    printf("*************************************************************\n");
    printf("*****************欢迎使用管理员模式**************************\n");  
    printf("*************************************************************\n");
    printf("* 1：查询  2：修改 3：添加用户  4：删除用户  5：查询历史记录*\n");
    printf("******************** 6:退出 *********************************\n");
    printf("*************************************************************\n");
}

//选择普通用户界面
void UI_Mode_User()
{    
    puts(""); 
    printf("*********************************************************\n"); 
    printf("*****************欢迎使用员工管理系统************************\n");   
    printf("***************1:查询  2:修改   3:退出*********************\n"); 
	printf("*********************************************************\n"); 
}


//管理员查找界面
//管理员添加界面
//管理员修改界面
//普通用户修改界面

//管理员查询接口
//管理员修改接口
//管理员添加用户接口
//管理员删除用户接口
//管理员查询历史记录接口
//管理员退出

//登陆接口

//用户查询接口
//用户修改接口
//用户退出

int main()
{
	int sockfd = 0;
	int acceptfd = 0;

	sockfd = socket(AF_INET,SOCK_STREAM,0);
	if(sockfd==-1)
	{
		perror("socket");
		return -1;
	}

	server_addr.sin_family = AF_INET;
	server_addr.sin_port = htons(atoi("8888"));
	server_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
	socklen_t server_addrlen = sizeof(server_addr);
	/*
	   if(bind(sockfd, (const struct sockaddr *)&server_addr,server_addrlen))
	   {
	   perror("bind");
	   return -1;
	   }
	*/
	if(connect(sockfd, (struct sockaddr *)&server_addr, server_addrlen)==-1)
	{
		perror("send");
		return -1;
	}

	char buf[128] = {0};
	size_t len = 0;
	char oper;
	Staff_Data_TypeDef data;
	staff_info_TypeDef userdata;
	//用户模式选择
	while(1)
	{	
		UI_Mode();
		printf(">>请选择");
		oper = fgetc(stdin);
		get_other_char(); 	//吃掉脏字符
		if('3' == oper){ 	//退出
			oper = 0;
			return 0;
		}else{
			printf("******************admin_or_user login******************\n");
			printf(">>输入用户名：");
			fgets(name,20,stdin);
			len = strlen(name);
			name[len-1]='\0';

			printf(">>输入密码：");
			fgets(pwd,20,stdin);
			len = strlen(pwd);
			pwd[len-1]='\0';
#if DEBUG_JUMP
			ERR_FLAG("直接跳出log，测试用");
			break; //直接雕出执行
#endif
			if('1' == oper){	//管理员模式
				if(!staff_admin_login(sockfd,USERMODE_ADMIN,name,pwd)){ //登陆成功退出
					break;
				}else{
					oper = 0;
					printf("登陆失败，请重新登陆....\n");
					continue;
				}
			}else if('2'==oper){	//用户模式
				if(!staff_admin_login(sockfd,USERMODE_COMMON,name,pwd)){ //登陆成功退出
					break;
				}else{
					oper = 0;
					printf("登陆失败，请重新登陆...\n");
					continue;
				}
			}else{					//
				oper = 0;
				printf("输入错误...\n");
				continue;
			}
		}
	}

	if('1' == oper){ //管理员页面
		int flag = 0;
		while(1){ //总页面
			UI_Mode_Root();
			printf(">>请选择");
			oper = fgetc(stdin);
			get_other_char(); 		//吃掉脏字符

			switch(oper){
			case '1': //查询
				#if DEBUG_JUMP
					ERR_FLAG("直接跳出登陆菜单，测试用");
					break; //直接雕出执行
				#endif
				while(1){ //查询页面
					printf("******************do_admin_query******************\n");
					printf("***1、按人名查找 2、查找所有 3、退出***\n");
					printf(">>请选择");
					oper = fgetc(stdin);
					get_other_char(); 	//吃掉脏字符
					if('1' == oper){
						memset(&userdata,0,sizeof(staff_info_TypeDef));
						if(!do_find_byname(sockfd,USERMODE_ADMIN,&userdata)){ //成功
							printf("工号\t用户类型\t姓名\t密码\t年龄\t电话\t地址\t职位\t入职年月\t等级\t工资\n");
							printf("==================================================================\n");
							printf("%d,\t%d,\t%s，\t%s,\t%d,\t%s，\t%s，\t%s，\t%s，\t%s，\t%d\n",
																userdata.id,userdata.mode,
																userdata.name,userdata.pwd,
																userdata.age,userdata.phone,
																userdata.addr,userdata.job,
																userdata.time,userdata.state,userdata.salary);
						}else{
							printf("查找信息失败\n");
						}
						oper = 0;
					}else if('2' == oper){
						if(!staff_admin_find_all(sockfd,USERMODE_ADMIN,NULL,NULL)){ //成功
							printf("查找成功\n");
						}else{
							printf("查找失败");
						}
						oper = 0;
					}else if('3' == oper){
						oper = 0;
						break; //退出查询页面，回到宗页面
					}
				}
				oper = 0;
				break;
			case '2': // 修改
				printf("修改页面,请添加代码...\n");
				#if DEBUG_JUMP
					ERR_FLAG("直接跳出修改菜单，测试用");
					break; //直接雕出执行
				#endif
				if(!do_modify_byname(sockfd,USERMODE_ADMIN)){
					flag =0;
				}else{
					printf("修改错误...\n");
				}
				oper = 0;
				break;
			case '3': //添加用户
				#if DEBUG_JUMP
					ERR_FLAG("直接跳出添加菜单，测试用");
					break; //直接雕出执行
				#endif
readd:
				if(!do_add_user(sockfd)){
					//是否继续添加
					printf("add success\n");
					flag = 0;
					printf(">>是否继续添加员工：(y/n):");
					oper = fgetc(stdin);
					get_other_char(); 		//吃掉脏字符
					if(oper == 'y'||oper == 'Y'){
						goto readd;
					}
				}else{
					printf("添加错误...\n");
				}
				oper = 0;
				break;
			case '4': //删除
				#if DEBUG_JUMP
					ERR_FLAG("直接跳出，测试用");
					break; //直接雕出执行
				#endif
				if(!do_del_user_by_id(sockfd)){
					flag = 0;
					printf("删除成功\n");
				}else{
					printf("删除错误...\n");
				}
				oper = 0;
				break;
			case '5': //查询历史记录
				if(!do_find_history(sockfd)){
					printf("查询历史记录成功\n");
					flag = 0;
				}else{
					printf("查询错误...\n");
				}
				oper = 0;
				break;
			case '6': //退出
				flag = 1;
				oper = 0;
				break;
			default:
			    oper = 0;
				break;
			}
			if(flag == 1){ //退出总菜单
				break;
			}
		}
		oper = 0;
	}else if('2' == oper){ //普通用户页面
		while(1){
			int ret = 0;
			UI_Mode_User();
			printf(">>请选择");
			oper = fgetc(stdin);
			get_other_char(); 	//吃掉脏字符
			if('1'==oper){ //查询
				memset(&userdata,0,sizeof(staff_info_TypeDef));
				if(!do_find_byname(sockfd,USERMODE_COMMON,&userdata)){
					printf("工号\t用户类型\t姓名\t密码\t年龄\t电话\t地址\t职位\t入职年月\t等级\t工资\n");
							printf("==================================================================\n");
							printf("%d,\t%d,\t%s，\t%s,\t%d,\t%s，\t%s，\t%s，\t%s，\t%s，\t%d\n",
																userdata.id,userdata.mode,
																userdata.name,userdata.pwd,
																userdata.age,userdata.phone,
																userdata.addr,userdata.job,
																userdata.time,userdata.state,userdata.salary);
				}else{
					printf("查找错误...\n");
				}
				continue;
			}else if('2'==oper){ //修改
				if(!(ret = do_modify_byname(sockfd,USERMODE_COMMON))){
					printf("修改成功\n");
				}else if(1 == ret){ //返回上一层菜单
					continue;
				}else{
					printf("修改失败...\n");
				}
				continue;
			}else if('3'==oper){ //退出
				break;
			}else{
				printf("输入错误，请重新输入...\n");
				continue;
			}
			break;
		}
		oper = 0;
	}
	else{
		printf("请检查代码...\n");
		oper = 0;
	}

	close(sockfd);
	return 0;
}


int do_find_byname(int sockfd,int usermode,staff_info_TypeDef *info_o)
{
	char lname[NAME_MAX] = {0};
	int len = 0;

	if(usermode == USERMODE_ADMIN){
		printf(">>请输入用户名：");
		fgets(lname,20,stdin);
		len = strlen(lname);
		lname[len-1]='\0';
	}else if(usermode == USERMODE_COMMON){
		strcpy(lname,name);
	}

	int ret = staff_admin_find_By_name(sockfd,usermode,lname,info_o);
	return ret;
}

int do_modify_byname(int sockfd,int usermode)
{
	staff_info_TypeDef data;
	int oper = 0;
	int len = 0;
	char cmd = 0;
	memset(&data,0,sizeof(staff_info_TypeDef));

	printf(">>请输入要您要修改的工号(只认工号)：");
	scanf("%d",&data.id);
	get_other_char();

	if(usermode==USERMODE_ADMIN){
		printf("*******************请输入要修改的选项********************\n");
		printf("******	1：姓名	  2：年龄	3：家庭住址   4：电话  ********\n");
		printf("******	5：职位	   6：工资  7：入职年月   8：评级  ********\n");
		printf("******	9：密码	 10：退出				       *********\n");
		printf("******************************************************\n");
		
		printf(">>请输入要修改的选项(数字):");
		scanf("%d",&oper);
		get_other_char();

		switch(oper){
		case 1:
			printf(">>请输入用户名:");
			fgets(data.name,NAME_MAX,stdin);
			len = strlen(data.name);
			data.name[len-1]='\0';
			cmd = 0x40;
			oper = 0;
			break;
		case 2:
			printf(">>请输入年龄:");
			scanf("%d",(int *)&data.age);
			get_other_char();
			cmd = 0x41;
			oper = 0;
			break;
		case 3:
			printf(">>请输入家庭住址:");
			fgets(data.addr,ADDR_MAX,stdin);
			len = strlen(data.addr);
			data.addr[len-1]='\0';
			cmd = 0x42;
			oper = 0;			
			break;
		case 4:
			printf(">>请输入电话:");
			fgets(data.phone,PHONE_MAX,stdin);
			len = strlen(data.phone);
			data.phone[len-1]='\0';
			cmd = 0x43;
			oper = 0;
			break;
		case 5:
			printf(">>请输入职位:");
			fgets(data.job,JOB_MAX,stdin);
			len = strlen(data.job);
			data.job[len-1]='\0';
			cmd = 0x44;
			oper = 0;
			break;
		case 6:
			printf(">>请输入工资:");
			scanf("%d",&data.salary);
			get_other_char();
			cmd = 0x45;
			oper = 0;
			break;
		case 7:
			printf(">>请输入入职日期(格式：2000.01.04):");
			fgets(data.time,TIME_MAX,stdin);
			len = strlen(data.time);
			data.time[len-1]='\0';
			cmd = 0x46;
			oper = 0;
			break;
		case 8:
			printf(">>请输入评级(1~5,5为最高，新员工为 1):");
			fgets(data.state,STATE_MAX,stdin);
			len = strlen(data.state);
			data.state[len-1]='\0';
			cmd = 0x47;
			oper = 0;
			break;
		case 9:
			printf(">>请输入用密码:");
			fgets(data.pwd,PWD_MAX,stdin);
			len = strlen(data.pwd);
			data.pwd[len-1]='\0';
			cmd = 0x48;
			oper = 0;
			break;
		case 10:  //返回上一层菜单，
			return 1;
		default: //其他 表示输入错误
			printf("选择错误，退出...\n");
			return 1;
		}

	}else if(usermode==USERMODE_COMMON){
		printf("***********请输入要修改的选项(其他信息亲请联系管理员)*********\n");
		printf("***********	1：家庭住址   2：电话  3：密码  4：退出*********\n");
		printf("*******************************************************\n");
		
		printf(">>请输入要修改的选项(数字):");
		scanf("%d",&oper);
		get_other_char();
		
		switch(oper){
		case 1:
			printf(">>请输入家庭住址:");
			fgets(data.addr,ADDR_MAX,stdin);
			len = strlen(data.addr);
			data.addr[len-1]='\0';
			cmd = 0x42;
			oper = 0;	
			break;
		case 2:
			printf(">>请输入电话:");
			fgets(data.phone,PHONE_MAX,stdin);
			len = strlen(data.phone);
			data.phone[len-1]='\0';
			cmd = 0x43;
			oper = 0;
			break;
		case 3:
			printf(">>请输入用密码:");
			fgets(data.pwd,PWD_MAX,stdin);
			len = strlen(data.pwd);
			data.pwd[len-1]='\0';
			cmd = 0x48;
			oper = 0;
			break;
		case 4:
			return 1;
		default:
			return 2;
		}
	}
	return staff_admin_modify(sockfd,usermode,cmd,&data);
}

int do_add_user(int sockfd)
{
	staff_info_TypeDef data;
	int len=0;
	char affirm;
	memset(&data,0,sizeof(staff_info_TypeDef));

s1:
	printf(">>请输入工号：");
	scanf("%d",&data.id);
	get_other_char();
	printf("您输入的工号是：%d\n",data.id);
	printf("工号信息一旦录入无法更改，请确认您的输入是否正确！(y/n):");
	affirm = fgetc(stdin);
	get_other_char();
	if(!(affirm=='Y'||affirm == 'y')){
		goto s1;
	}

	printf(">>请输入用户名:");
	fgets(data.name,NAME_MAX,stdin);
	len = strlen(data.name);
	data.name[len-1]='\0';

	printf(">>请输入用密码:");
	fgets(data.pwd,PWD_MAX,stdin);
	len = strlen(data.pwd);
	data.pwd[len-1]='\0';

	printf(">>请输入年龄:");
	scanf("%d",(int *)&data.age);
	get_other_char();

	printf(">>请输入电话:");
	fgets(data.phone,PHONE_MAX,stdin);
	len = strlen(data.phone);
	data.phone[len-1]='\0';

	printf(">>请输入家庭住址:");
	fgets(data.addr,ADDR_MAX,stdin);
	len = strlen(data.addr);
	data.addr[len-1]='\0';

	printf(">>请输入职位:");
	fgets(data.job,JOB_MAX,stdin);
	len = strlen(data.job);
	data.job[len-1]='\0';

	printf(">>请输入入职日期(格式：2000.01.04):");
	fgets(data.time,TIME_MAX,stdin);
	len = strlen(data.time);
	data.time[len-1]='\0';

	printf(">>请输入评级(1~5,5为最高，新员工为 1):");
	fgets(data.state,STATE_MAX,stdin);
	len = strlen(data.state);
	data.state[len-1]='\0';

	printf(">>请输入工资:");
	scanf("%d",&data.salary);
	get_other_char();

	printf(">>请输入是否为管理员(Y/N):");
	affirm = fgetc(stdin);
	get_other_char();
	if((affirm=='Y'||affirm == 'y')){
		data.mode = 0;
	}else{
		data.mode = 1;
	}
	return staff_admin_add(sockfd,USERMODE_ADMIN,&data);	
}


int do_del_user_by_id(int sockfd)
{
	int id;
	char name[NAME_MAX]={0};
	int len=0;


	printf(">>请输入要删除的工号：");
	scanf("%d",&id);
	get_other_char();

	printf(">>请输入要删除的用户名");
	fgets(name,NAME_MAX,stdin);
	len = strlen(name);
	name[len-1]='\0';

	return staff_admin_del_by_id(sockfd,USERMODE_ADMIN,id,name);	
}


int do_find_history(int sockfd)
{
	return staff_admin_find_history(sockfd,USERMODE_ADMIN);
}


static void get_other_char(void)
{
	char ch;
	while('\n'!=(ch=fgetc(stdin)));
}
